<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>修改密码</title>
	<script src="resources/js/public/jquery-3.3.1.min.js"></script>
	<script src="resources/layui/layui.js"></script>
	<script src="resources/js/public/jquery.session.js"></script>
	<script src="resources/js/public/vue.min.js"></script>
	<link rel="stylesheet" href="resources/layui/css/layui.css">
</head>
<body>
	<div style="width: 50%;margin:250px auto">
		<h2 id="userInfo" style="margin-bottom:20px;">ID:{{id}} &nbsp;&nbsp;  Name:{{name}}</h2>
		<form class="layui-form">
			<div class="layui-form-item">
				<label class="layui-form-label">初始密码:</label>
				<div class="layui-input-block">
					<input id="originPW" type="password" required  lay-verify="required" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">新密码:</label>
				<div class="layui-input-block">
					<input id="newPW" type="password" required  lay-verify="required" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">确认密码:</label>
				<div class="layui-input-block">
					<input id="againPW" type="password" required  lay-verify="required" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item" style="margin-top: 40px">
				<button type="button"  lay-submit class="btn layui-btn layui-btn-normal layui-btn-fluid"  id="confirm">确认修改</button>
			</div>
		</form>
	</div>
</body>
<script>
    layui.use(["form","layer"],function () {
        var form = layui.form;
        var layer = layui.layer;

        var userInfo=new Vue({
			el:"#userInfo",
			data:{
			    id:$.session.get("id"),
				name:$.session.get("name"),
			}
		});


        $(document).on("click","#confirm",function () {
            var id=$.session.get("id");
            var identity=$.session.get("identity");
            var originPW=$("#originPW").val();
            var newPW=$("#newPW").val();
            var againPW=$("#againPW").val();
            $.ajax({
				url:"confirmOriginPW",
				type:"post",
				data:{
				    "id":id,
				    "orignPW":originPW,
				},
				success:function (data) {
					if(1==data){
                        if(newPW!=againPW){
                            layer.msg("两次输入密码不相同!!");
						}
                        //初始密码验证通过,且新密码和确认密码相同,发送修改请求
                        else if((newPW==againPW)){
                            $.ajax({
                                url:"modifyPW",
                                type:"post",
                                data:{
                                    "id":id,
									"identity":identity,
                                    "newPW":newPW,
                                },
                                success:function (data) {
                                    if(1==data){
                                        layer.msg("修改密码成功!!");
                                    }
                                    else if(0==data){
                                        layer.msg("修改密码失败!!");
                                    }
                                }
                            });
						}
					}
					else if(0==data){
					    layer.msg("初始密码错误");
					}
                },
			});
        });


    });
</script>
</html>